<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>地理边界聚合 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/geo-bounds-agg.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/geo-bounds-agg.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-bounds-agg.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-bounds-agg.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="geoloc.html">地理位置</a></span>
»
<span class="breadcrumb-link"><a href="geo-aggs.html">地理位置聚合</a></span>
»
<span class="breadcrumb-node">地理边界聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="geohash-grid-agg.html">« Geohash 网格聚合</a>
</span>
<span class="next">
<a href="geo-shapes.html">地理形状 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="geo-bounds-agg"></a>地理边界聚合 (Geo Bounds Aggregation)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/330_Geo_aggs/66_Geo_bounds_agg.asciidoc">edit</a>
</h2>
</div></div></div>
<p>在我们<a class="xref" href="geohash-grid-agg.html" title="Geohash 网格聚合">之前的例子</a>中，我们通过一个覆盖大纽约区的边框来过滤结果。 然而，我们的结果全部都位于曼哈顿市中心。当为我们的用户显示一个地图的时候，放大包含数据的区域是有意义的；展示大量的空白空间是没有任何意义的。</p>
<p><code class="literal">geo_bounds</code> 正好是这样的：它计算封装所有地理位置点所需要的最小边界框：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/restaurant/_search
{
  "size" : 0,
  "query": {
    "constant_score": {
      "filter": {
        "geo_bounding_box": {
          "location": {
            "top_left": {
              "lat":  40,8,
              "lon": -74.1
            },
            "bottom_right": {
              "lat":  40.4,
              "lon": -73.9
            }
          }
        }
      }
    }
  },
  "aggs": {
    "new_york": {
      "geohash_grid": {
        "field":     "location",
        "precision": 5
      }
    },
    "map_zoom": { <a id="CO242-1"></a><i class="conum" data-value="1"></i>
      "geo_bounds": {
        "field":     "location"
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO242-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">geo_bounds</code> 聚合将计算封装所有匹配查询文档所需要的最小边界框。</p>
</td>
</tr>
</table>
</div>
<p>响应数据现在已经包含了一个可以用来缩放地图的边界框：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">...
"aggregations": {
  "map_zoom": {
     "bounds": {
        "top_left": {
           "lat":  40.722,
           "lon": -74.011
        },
        "bottom_right": {
           "lat":  40.715,
           "lon": -73.983
        }
     }
  },
...</pre>
</div>
<p>事实上，我们甚至可以在每一个 geohash 单元格内部使用 <code class="literal">geo_bounds</code> 聚合， 以免一个单元格内的地理位置点仅集中在单元格的某一块：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/restaurant/_search
{
  "size" : 0,
  "query": {
    "constant_score": {
      "filter": {
        "geo_bounding_box": {
          "location": {
            "top_left": {
              "lat":  40,8,
              "lon": -74.1
            },
            "bottom_right": {
              "lat":  40.4,
              "lon": -73.9
            }
          }
        }
      }
    }
  },
  "aggs": {
    "new_york": {
      "geohash_grid": {
        "field":     "location",
        "precision": 5
      },
      "aggs": {
        "cell": { <a id="CO243-1"></a><i class="conum" data-value="1"></i>
          "geo_bounds": {
            "field": "location"
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO243-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">cell_bounds</code> 子聚合会为每个 geohash 单元计算边界框。</p>
</td>
</tr>
</table>
</div>
<p>现在, 每个单元格里的点都有一个边界框：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">...
"aggregations": {
  "new_york": {
     "buckets": [
        {
           "key": "dr5rs",
           "doc_count": 2,
           "cell": {
              "bounds": {
                 "top_left": {
                    "lat":  40.722,
                    "lon": -73.989
                 },
                 "bottom_right": {
                    "lat":  40.719,
                    "lon": -73.983
                 }
              }
           }
        },
...</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="geohash-grid-agg.html">« Geohash 网格聚合</a>
</span>
<span class="next">
<a href="geo-shapes.html">地理形状 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>